package com.blog.controller;

import com.blog.check.result.Result;
import com.blog.check.resultenum.UserChkRsEnum;
import com.blog.dao.query.BlogTypeQuery;
import com.blog.form.BlogTypeForm;
import com.blog.service.service.BlogTypeService;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 博客类型控制器
 *
 * @author 泡面ing
 * @date 2021/11/22
 */
@Controller
@RequestMapping("/blogType")
public class BlogTypeController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private BlogTypeService blogTypeService;

    @Autowired
    public void setBlogTypeService(BlogTypeService blogTypeService){
        this.blogTypeService = blogTypeService;
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Result list(BlogTypeQuery blogTypeQuery, HttpServletResponse response){
        Result result;
        try{
            result = blogTypeService.list(blogTypeQuery,1,10);
            if (result.getCode() == UserChkRsEnum.USER_IS_NOT_LOGIN.getCode()){
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                return result;
            }
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("查询信息异常",e);
        }
        return result;
    }

    @RequestMapping(value = "findByName",method = RequestMethod.PUT)
    @ResponseBody
    public Result findByName(@RequestBody BlogTypeQuery blogTypeQuery, HttpServletResponse response){
        Result result;
        try{
            result = blogTypeService.findByName(blogTypeQuery);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("查询信息异常",e);
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody BlogTypeForm blogTypeForm, HttpServletResponse response){
        Result result;
        try{
            result = blogTypeService.save(blogTypeForm);
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("添加信息异常");
        }
        return result;
    }

    @RequestMapping(value = "/{blogType_id}",method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@PathVariable("blogType_id") String blogTypeId, @RequestBody BlogTypeForm blogTypeForm, HttpServletResponse response){
        Result result;
        try {
            result = blogTypeService.update(blogTypeId,blogTypeForm);
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_OK);
        }catch(Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("更新信息异常");
        }
        return result;
    }

    @RequestMapping(value = "/{blogType_id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Result remove(@PathVariable("blogType_id") String blogTypeIid, HttpServletResponse response){
        Result result;
        try{
            result = blogTypeService.remove(blogTypeIid);
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_OK);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("更新信息异常");
        }
        return  result;
    }
}
